package com.nameless.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nameless.entity.*;
import com.nameless.feign.CarFeigns;
import com.nameless.feign.ClientFeign;
import com.nameless.feign.ShopFeign;
import com.nameless.service.OrderService;
import com.nameless.util.JwtUtil;
import com.nameless.util.WebUtils;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 陈灰灰
 * @Date: 2023/03/02/19:00
 * @Description:
 */
@Api(value = "/order/api/", tags = {"订单业务控制层"})
@RestController
@Slf4j
@RequestMapping("/order/api/")
public class ApiOrderController {
    @Autowired
    private OrderService orderService;
    @Resource
    private CarFeigns carFeign;
    @Resource
    private ClientFeign clientFeign;
    @Resource
    private ShopFeign shopFeign;

    /**
     * @Author chh
     *通过客户Id查询订单
     * @return {@link Result}
     */
    @ApiOperation(value = "通过客户Id查询订单", notes = "通过客户Id查询订单")
    @RequestMapping("/getOrders")
    public Result getOrders(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String clientName = tokenChaim.get("clientname").toString();
        Client client = clientFeign.getClientName(clientName);
        List<Order> orders = orderService.getOrders(client.getId());
        List<Order> orderList = orders.stream().map(item -> {
            Integer carId = item.getCarId();
            Car car = carFeign.selectId(carId);
            if (car != null) {
                item.setCar(car);
            }
            return item;
        }).collect(Collectors.toList());

        return new Result(200,"查询成功",orderList);
    }

    /**
     * @Author chh
     * 通过id获取订单
     * @param id id
     * @return {@link Result}
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "id", required = true)
    })
    @ApiOperation(value = "通过id获取订单", notes = "通过id获取订单")
    @RequestMapping("/getOrder/{id}")
    public Result getOrderById(@PathVariable Integer id){
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId,id);
        Order order = orderService.getOne(queryWrapper);
        System.out.println(order);
        Car car = carFeign.selectId(order.getCarId());
        System.out.println(car);
        CarType carType = carFeign.getTypeName(car.getId());
        Shop shop = shopFeign.getShopById(car.getShopId());
        System.out.println(shop);
        CarInsurance carInsurance = carFeign.getCarInsuranceById(car.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("order",order);
        map.put("car",car);
        map.put("carType",carType);
        map.put("carInsurance",carInsurance);
        map.put("shop",shop);
        return new Result(200,"查询成功",map);
    }
}
